package test05;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.dozer.DozerBeanMapper;

/**
 * 基本+复杂类型：一方属性是基本类型，而另一方是复杂类型，且双方属性名称都不相同
 * @author Administrator
 *
 */
public class DozerTest05 {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
		
		/**
		 * PO或VO，有一方的属性是复杂类型，而另一方是基本类型：必须得配置XML
		 */
		DozerBeanMapper mapper = new DozerBeanMapper();
		
		List mf = new ArrayList();
		mf.add("test05/test05.xml");//格式：“目录/XML文件名”，若无目录时默认为ClassPath目录
		mapper.setMappingFiles(mf);

		//示例一：UserPO -> UserVO
		UserPO po1 = new UserPO();
		Info info1 = new Info();
		info1.setId(100);
		info1.setName("123");
		info1.setAge(10);
		info1.setHeight(170.5F);
		info1.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2008-08-08"));
		po1.setInfo(info1);
		
		UserVO vo1 = mapper.map(po1, UserVO.class);

		System.out.println("示例一：由PO向VO转换");
		System.out.println("UserPO	->	UserVO");
		
		System.out.println(po1.getInfo().getId()+"[int]    "+vo1.getId2()+"[Integer]");
		System.out.println(po1.getInfo().getName()+"[String]    "+vo1.getName2()+"[int]");
		System.out.println(po1.getInfo().getAge()+"[Integer]    "+vo1.getAge2()+"[String]");
		System.out.println(po1.getInfo().getHeight()+"[float]    "+vo1.getHeight2()+"[String]");
		System.out.println(po1.getInfo().getBirthday()+"[Date]    "+vo1.getBirthday2()+"[String]");
		
		//*
		//示例二：UserVO -> UserPO
		UserVO vo2 = new UserVO();
		vo2.setId2(null);
		vo2.setName2(123);
		vo2.setAge2("20");
		vo2.setHeight2("170.5F");
		vo2.setBirthday2("2010-10-10");//String -> Date：需要配置XML
		
		UserPO po2 = new UserPO();
		mapper.map(vo2, po2);
		//UserPO po2 = mapper.map(vo2, UserPO.class);

		System.out.println("\n示例二：由VO向PO转换");
		System.out.println("UserVO	->	UserPO");
		System.out.println(vo2.getId2()+"[Integer]    "+po2.getInfo().getId()+"[int]");
		System.out.println(vo2.getName2()+"[int]    "+po2.getInfo().getName()+"[String]");
		System.out.println(vo2.getAge2()+"[String]    "+po2.getInfo().getAge()+"[Integer] ");
		System.out.println(vo2.getHeight2()+"[String]    "+po2.getInfo().getHeight()+"[float]");
		System.out.println(vo2.getBirthday2()+"[String]    "+po2.getInfo().getBirthday()+"[Date]");
		//*/
		/*
		 projectTypeList和subList都是List对象，且都属性同一类型。
		 且都有code和parmValue属性，
		 其中subList是projectTypeList的属性List，用于级联显示下拉很有用.如部门和子部门等：
								<s:iterator value="projectTypeList">
									<option value="<s:property value="code"/>">
										<s:property value="parmValue" />
									</option>
									<s:if test="subList!=null">
										<s:iterator value="subList">
											<option value="<s:property value="code"/>">
												&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|---<s:property value="parmValue" />
											</option>
										</s:iterator>
									</s:if>
								</s:iterator>
								
		 */
	}
}
